//
//  NumberTitleButton.m
//  QA
//
//  Created by 李小龙 on 2017/5/12.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "NumberTitleButton.h"

@interface NumberTitleButton()
{
    BOOL _selected;
}
@end

@implementation NumberTitleButton

- (instancetype)initWithFrame:(CGRect)frame
                       number:(NSString *)number
                        title:(NSString *)title
                showRightLine:(BOOL)showRight
                   messageNum:(NSString *)messageNum
                 showRedPoint:(BOOL)showRedPoint {
    self = [super initWithFrame:frame];
    if (self) {
        _selected = NO;
        // 文字label
        _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [_textLabel setFont:[UIFont systemFontOfSize:12.0f]];
        [_textLabel setTextColor:[UIColor colorWithHex:0x666666 alpha:1.0]];
        [_textLabel setTextAlignment:NSTextAlignmentCenter];
        [_textLabel setText:title];
        
        [self addSubview:_textLabel];
        // 右边灰线
        _rightLineView = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width-0.5, 12.5f, 0.5, frame.size.height-25.0f)];
        [_rightLineView setBackgroundColor:[UIColor borderColor]];
        [_rightLineView setFrame:CGRectMake(frame.size.width-0.5, 12.5f, 0.5, frame.size.height-25.f)];
        
        [self addSubview:_rightLineView];
        if (showRight) {
            [self showRightLine];
        } else {
            [self hiddenRightLine];
        }
        
        if (TextIsNotNil(number)) {
            // 数字label
            _numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 12.0f)];
            [_numberLabel setFont:[UIFont systemFontOfSize:12.0f]];
            [_numberLabel setText:number];
            [_numberLabel setTextColor:[UIColor whiteColor]];
            [_numberLabel setTextAlignment:NSTextAlignmentCenter];
            [self addSubview:_numberLabel];
            // 文字
            [_textLabel setFont:[UIFont systemFontOfSize:12.0f]];
            [_textLabel setFrame:CGRectMake(0, frame.size.height-12.0f, frame.size.width, 12.0f)];
            [_textLabel setTextColor:[UIColor borderColor]];
//            // 右线
//            [_rightLineView setBackgroundColor:[UIColor borderColor]];
//            [_rightLineView setFrame:CGRectMake(frame.size.width-0.5, 6.0f, 0.5, frame.size.height-12.0f)];
            
        } else {
            
            // 右线
//            CAGradientLayer *gradientLayer = [CAGradientLayer layer];
//            gradientLayer.colors = @[(__bridge id)[UIColor colorWithHex:0xE7E7E7 alpha:1.0].CGColor, (__bridge id)[UIColor borderColor].CGColor, (__bridge id)[UIColor colorWithHex:0xE7E7E7 alpha:1.9].CGColor];
//            gradientLayer.locations = @[@0, @0.5, @1.0];
//            gradientLayer.startPoint = CGPointMake(0, 0);
//            gradientLayer.endPoint = CGPointMake(0, 1.0);
//            gradientLayer.frame = CGRectMake(0, 0, _rightLineView.width, _rightLineView.height);
//            [_rightLineView.layer addSublayer:gradientLayer];

        }
        [_textLabel sizeToFit];
        [_textLabel setCenter:CGPointMake(self.width/2, self.height/2)];
        
        
        // 红点
        _redPoint = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
        [_redPoint setTextColor:[UIColor whiteColor]];
        [_redPoint setFont:[UIFont systemFontOfSize:8.0f]];
        [_redPoint setTextAlignment:NSTextAlignmentCenter];
        [_redPoint setBackgroundColor:[UIColor colorWithHex:0xFF0000 alpha:1.0]];
        [_redPoint setClipsToBounds:YES];
        [_redPoint setHalfCircleWithColor:[UIColor clearColor]];
        
        [self setRedPointText:messageNum];
        [self addSubview:_redPoint];
        if (!showRedPoint || [messageNum isEqualToString:@"0"]) {
            [self hiddenRedPoint];
        } else {
            [self showRedPoint];
        }
        
        [self addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
- (void)setRedPointText:(NSString *)text {
    if (text.integerValue > 99) {
        text = @"99+";
    }
    [_redPoint setText:text];
    [_redPoint sizeToFit];
    [_redPoint setHeight:10];
    
    if (_redPoint.width <= 10) {
        [_redPoint setWidth:10];
    } else {
        [_redPoint setWidth:_redPoint.width+5];
    }
    
    [_redPoint setCenter:CGPointMake(_textLabel.centerX+_textLabel.width/2+_redPoint.width/2, _textLabel.centerY-_redPoint.height/2)];
}
- (void)showRedPoint {
    [self.redPoint setHidden:NO];
}
- (void)hiddenRedPoint {
    [self.redPoint setHidden:YES];
}
- (void)showRightLine {
    [self.rightLineView setHidden:NO];
}
- (void)hiddenRightLine {
    [self.rightLineView setHidden:YES];
}
//MARK:目前需求是button只有文字的时候才有点击事件，文字和数字都有时不点，以后有需求再改
- (void)buttonAction:(id)sender {
    NumberTitleButton *button = (NumberTitleButton *)sender;
    [button setSelected:YES];
//    [self hiddenRedPoint];
    if (button.selected) {
        [button.textLabel setTextColor:[UIColor orangePriceColor]];
    } else {
        [button.textLabel setTextColor:[UIColor colorWithHex:0x666666 alpha:1.0]];
    }
    if (self.block) {
        self.block(button.tag);
    }
}
//#pragma mark - set get
- (void)setSelected:(BOOL)selected {
    _selected = selected;
    if (_selected) {
        [self.textLabel setTextColor:[UIColor orangePriceColor]];
    } else {
        [self.textLabel setTextColor:[UIColor colorWithHex:0x666666 alpha:1.0]];
    }
}
- (BOOL)isSelected {
    return _selected;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
